package com.briup.smart.evn.client;

import com.briup.smart.env.client.Gather;
import com.briup.smart.env.entity.Environment;
import com.sun.xml.internal.ws.model.wsdl.WSDLOutputImpl;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;



public class GatherImpl implements Gather {
    @Override
    public Collection<Environment> gather() throws Exception {
//        D:\xxx\EnvDataCenter\env-gather-impl\src\main\resources\data-file-simple

//        File file = new File("env-gather-impl\\src\\main\\resources\\data-file-simple");
//        System.out.println("文件是否存在"+file.exists());
        String filePath = "env-gather-impl/src/main/resources/data-file-simple";

        ArrayList<Environment> list = new ArrayList<>();
        try (
                BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath));
        ) {
            String str = null;

            while ((str = bufferedReader.readLine()) != null) {

                String[] arr = str.split("[|]");
                Environment environment = new Environment();
                environment.setSrcId(arr[0]);
                environment.setDesId(arr[1]);
                environment.setDevId(arr[2]);
                environment.setSensorAddress(arr[3]);
                environment.setCount(Integer.parseInt(arr[4]));
                environment.setCmd(arr[5]);
                environment.setStatus(Integer.parseInt(arr[7]));
                environment.setGatherDate(new Timestamp(Long.parseLong(arr[8])));

            switch (arr[3]) {
                case "16":
                    environment.setName("温度");
                    String temperature = arr[6].substring(0, 4);
                    int t = Integer.parseInt(temperature,16);
                    environment.setData((t * (0.00268127F)) - 46.85F);
                    list.add(environment);
                    Environment environment1 = cloneEnvironment(environment);
                    environment1.setName("湿度");
                    String humidity = arr[6].substring(4,8);
                    int h = Integer.parseInt(humidity,16);
                    environment1.setData((h * 0.00190735F) - 6);
                    list.add(environment1);
                    break;
                case "256":
                    environment.setName("光照强度");
                    environment.setData(Integer.parseInt(arr[6].substring(0,4),16));
                    list.add(environment);
                    break;
                case "1280":
                    environment.setName("二氧化碳浓度");
                    environment.setData(Integer.parseInt(arr[6].substring(0,4),16));
                    list.add(environment);
                    break;
                default:
                    System.out.println("数据格式错误" + str);
                    break;
            }
            }
        }
//        return Collections.emptyList();
        catch (IOException e) {
            e.printStackTrace();

        }
        System.out.println("采集模块：采集数据完成，本次采集：" + list.size() + "条");

        return list;
    }
    private Environment cloneEnvironment(Environment e) {
              return new Environment(null, e.getSrcId(), e.getDesId(),
                e.getDevId(), e.getSensorAddress(), e.getCount(), e.getCmd(),
                e.getStatus(), 0, e.getGatherDate());
    }


}
